package com.cimr.comm.websocket;

import org.springframework.web.socket.server.standard.SpringConfigurator;

import javax.websocket.*;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;

/**
 * Created by liqi on 2018/4/25.
 * liqiwork@qq.com
 */
@SuppressWarnings("all")
@ServerEndpoint(value="/websocket/{deviceId}" ,configurator = SpringConfigurator.class)
public class WebSocketServer {

    /**
     * 接收信息事件
     * @param message 客户端发来的消息
     * @param session 当前会话
     */
    @OnMessage
    public void onMessage(String message, Session session, @PathParam(value="deviceId") String deviceId) {
        System.out.println("======================用户的webSockt连接发来信息=====================");
        System.out.println(message);
        session.getAsyncRemote().sendText("收到"+message);
    }

    /**
     * 打开连接事件
     * @throws Exception
     */
    @OnOpen
    public void onOpen(Session session, @PathParam(value="deviceId") String deviceId) {
        System.out.println("======================用户已经建立了webSockt连接=====================");
    }

    /**
     * 关闭连接事件
     */
    @OnClose
    public void onClose(Session session,@PathParam(value="deviceId")String deviceId) {
        System.out.println("======================用户断开了webSockt连接=====================");
    }

    /**
     * 错误信息响应事件
     * @param session
     * @param throwable
     */
    @OnError
    public void OnError(Session session, Throwable throwable, @PathParam(value="deviceId") String deviceId) {
        System.out.println("======================用户的webSockt连接发生了异常=====================");
    }
}
